package com.wanmait.zaisousuo.util;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.sun.org.apache.bcel.internal.classfile.Code;
import org.apache.commons.mail.HtmlEmail;
import org.json.JSONObject;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import java.util.Random;

public class CodeUtil {
        //随机生成6位验证码
        public static String getCode() {
            String code ="";
            Random r = new Random();
            for (int i=0;i<6;i++){
                code=code+ r.nextInt(9)+"";
            }

            return code;
        }
        //随机生成昵称
        public static String randomNickname ()
        {
            String code = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890";
            String codes = "";
            Random r = new Random();
            for (int i=0;i<4;i++ ) {
                int index = r.nextInt(code.length());
                char n = code.charAt(index);
                codes = codes+n;
            }
            return codes;
        }

        //手机注册验证
        public static String  aliyun(String phoneNumber)
        {
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIraswqymIsm4c", "muSDoPAq6CjhxAQMBi1Iw6XQpg4tWR");
            IAcsClient client = new DefaultAcsClient(profile);

            JsonParser json = new JsonParser();
            String code = CodeUtil.getCode();
            json.setCode(code);
            String codeJson = JSON.toJSONString(json);
            CommonRequest request = new CommonRequest();
            request.setSysMethod(MethodType.POST);
            request.setSysDomain("dysmsapi.aliyuncs.com");
            request.setSysVersion("2017-05-25");
            request.setSysAction("SendSms");
            request.putQueryParameter("RegionId", "cn-hangzhou");
            request.putQueryParameter("PhoneNumbers", phoneNumber);
            request.putQueryParameter("SignName", "再搜索开发版");
            request.putQueryParameter("TemplateCode", "SMS_169103708");
            request.putQueryParameter("TemplateParam", codeJson);
            try {
                CommonResponse response = client.getCommonResponse(request);
                System.out.println(response.getData());
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
            return code;
        }

        //手机短信验证码
        public static void  phoneCode(String phoneNumber,String companyName,String reason)
        {
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIraswqymIsm4c", "muSDoPAq6CjhxAQMBi1Iw6XQpg4tWR");
            IAcsClient client = new DefaultAcsClient(profile);

            String reasons = "{\"mtname\":\""+companyName+"\",\"reason\":\""+reason+"\"}";
            CommonRequest request = new CommonRequest();
            request.setSysMethod(MethodType.POST);
            request.setSysDomain("dysmsapi.aliyuncs.com");
            request.setSysVersion("2017-05-25");
            request.setSysAction("SendSms");
            request.putQueryParameter("RegionId", "cn-hangzhou");
            request.putQueryParameter("PhoneNumbers", phoneNumber);
            request.putQueryParameter("SignName", "再搜索用户认证通知");
            request.putQueryParameter("TemplateCode", "SMS_172220084");
            request.putQueryParameter("TemplateParam", reasons);
            try {
                CommonResponse response = client.getCommonResponse(request);
                System.out.println(response.getData());
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }

        }
        // SSL 发送邮件
        public static void sendEmail(String to,String message,String subject)
        {

            try {
                Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
                final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
                //设置邮件会话参数
                Properties props = new Properties();
                //邮箱的发送服务器地址
                props.setProperty("mail.smtp.host", "smtp.mxhichina.com");
                props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
                props.setProperty("mail.smtp.socketFactory.fallback", "false");
                //邮箱发送服务器端口,这里设置为465端口
                props.setProperty("mail.smtp.port", "465");
                props.setProperty("mail.smtp.socketFactory.port", "465");
                props.put("mail.smtp.auth", "true");
                final String username = "webmaster@qinglab.cn";
                final String password = "LzYl007993";
                //获取到邮箱会话,利用匿名内部类的方式,将发送者邮箱用户名和密码授权给jvm
                Session session = Session.getDefaultInstance(props, new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
                //通过会话,得到一个邮件,用于发送
                Message msg = new MimeMessage(session);
                //设置发件人
                msg.setFrom(new InternetAddress("webmaster@qinglab.cn","再搜索垃圾分类网","UTF-8"));
                //设置收件人,to为收件人,cc为抄送,bcc为密送
                msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
                msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(to, false));
                msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(to, false));
                msg.setSubject(subject);
                //设置邮件消息
                msg.setText(message);
                //设置发送的日期
                msg.setSentDate(new Date());

                //调用Transport的send方法去发送邮件
                Transport.send(msg);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    /**
     * 使用加密的方式,利用465端口进行传输邮件,开启ssl
     * @param to    为收件人邮箱
     */
    public static String inputEmail(String to,String message,String subject) {
        String code = CodeUtil.getCode();
        sendEmail(to,message+code,subject);
            return code;
    }
    }



